After several tries which did not work: This required installing the latest matching version of ffmpeg from ( https://launchpad.net/~jon-severinsson/+archive/ffmpeg ) on Ubuntu 12.04 and then passing the extra arguments extra_args=['-acodec', 'libfaac', '-vcodec', 'libx264'] to an explicitly instantiated FFMpegWriter instead of using animation.save(...)
Here is the ffmpeg version I finally installed:
In [3]:
!ffmpeg -version
In [29]:
import numpy as np
import matplotlib
#matplotlib.use("Agg")
import matplotlib.pyplot as plt
from math import sin,cos,pi
import matplotlib.animation as manimation
# If we set matplotlib logging level to debug we get more meaningful error reports if the FFMPegWriter fails.
# but we have to set it to "helpful" or "silent" in order to prevent UnsupportedOperation: IOStream has no fileno
# errors
#matplotlib.verbose.set_level('debug')
matplotlib.verbose.set_level('helpful')
FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Movie Test', artist='Matplotlib',
comment='Movie support!')
writer = FFMpegWriter(fps=15, metadata=metadata, extra_args=['-acodec', 'libfaac', '-vcodec', 'libx264'])
x0,y0 = 0, 0
fig = plt.figure()
with writer.saving(fig, "writer_test4.mp4", 100):
for i in range(100):
x0 += 0.1 * np.random.randn() + 0.05*sin(i * 2.0 * pi / 100)
y0 += 0.1 * np.random.randn() + 0.05*cos(i * 2.0 * pi / 100)
fig.clear()
plt.xlim(-5, 5)
plt.ylim(-5, 5)
l, = plt.plot(x0, y0, 'k-o')
#l.set_data(x0, y0)
writer.grab_frame()
In [30]:
from IPython.display import HTML
from base64 import b64encode
video = open("writer_test4.mp4", "rb").read()
video_encoded = b64encode(video).decode('ascii')
video_tag = '<video controls alt="test" src="data:video/x-m4v;base64,{0}">'.format(video_encoded)
HTML(data=video_tag)
Out[30]: